package cn.chiship.framework.upms.biz.user.controller;


import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.upms.biz.user.entity.UpmsRolePermission;

import cn.chiship.framework.upms.biz.user.entity.UpmsRolePermissionExample;
import cn.chiship.framework.upms.biz.user.service.UpmsRolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * controller
 *
 * @author lijian
 * @date 2020/3/13
 */
@RestController
@Authorization
@RequestMapping("/rolePermission")
@Api(tags = "角色权限关联控制器")
public class UpmsRolePermissionController extends BaseController<UpmsRolePermission, UpmsRolePermissionExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsRolePermissionController.class);

    @Autowired
    private UpmsRolePermissionService upmsRolePermissionService;

    @Override
    public BaseService getService() {
        return upmsRolePermissionService;
    }

    @SystemOptionAnnotation(describe = "获取指定角色下的权限")
    @ApiOperation(value = "获取指定角色下的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping(value = "getPermissionByRoleId")
    public ResponseEntity<BaseResult> getPermissionByRoleId(@RequestParam("roleId") String roleId) {
        return super.responseEntity(upmsRolePermissionService.getPermissionByRoleId(roleId));
    }

    @SystemOptionAnnotation(describe = "保存分配的权限", option = BusinessTypeEnum.SYSTEM_OPTION_SAVE)
    @ApiOperation(value = "保存分配的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataTypeClass = String.class, paramType = "path"),
    })
    @PostMapping(value = "rolePermissionSave/{roleId}")
    public ResponseEntity<BaseResult> rolePermissionSave(
            @PathVariable("roleId") String roleId,
            @RequestBody @Valid List<String> permissionIdList) {
        return super.responseEntity(upmsRolePermissionService.rolePermissionSave(roleId, permissionIdList));
    }

    @SystemOptionAnnotation(describe = "删除分配的权限", option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE)
    @ApiOperation(value = "删除分配的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataTypeClass = String.class, paramType = "path"),
    })
    @PostMapping(value = "rolePermissionRemove/{roleId}")
    public ResponseEntity<BaseResult> rolePermissionRemove(
            @PathVariable("roleId") String roleId,
            @RequestBody @Valid List<String> permissionIdList) {
        return super.responseEntity(upmsRolePermissionService.rolePermissionRemove(roleId, permissionIdList));
    }
}
